package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletChangeRechargeContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.PayResultBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletChangeRechargePresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletChangeRechargeActivity extends BaseToolbarActivity implements WalletChangeRechargeContract.View {
    @Inject
    WalletChangeRechargePresenter mPresenter;
    private TextView mTvPrice;
    private LinearLayout mLlAlipay;
    private LinearLayout mLlWeChat;
    private ImageView mIvAlipay;
    private ImageView mIvWeChat;
    private Button mBtnConfirm;
    private String stringExtra;
    private Dialog mDialog;


    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);

        initId();
        initInfo();
        setTbTitle(getString(R.string.wallet_change_recharge_title));
        isEnable(false);
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_change_recharge;
    }

    @Override
    protected void onRefreshData() {

    }

    /**
     * 初始化Id
     */
    private void initId() {
        mTvPrice = getViewById(R.id.tv_wallet_change_recharge_order_price);
        mLlAlipay = getViewById(R.id.ll_wallet_change_recharge_alipay);
        mLlWeChat = getViewById(R.id.ll_wallet_change_recharge_wechat);
        mIvAlipay = getViewById(R.id.iv_wallet_alipay_select);
        mIvWeChat = getViewById(R.id.iv_wallet_wechat_select);
        mBtnConfirm = getViewById(R.id.btn_wallet_change_recharge_confirm);
        RxUtils.bindEvents(mLlAlipay, this);
        RxUtils.bindEvents(mLlWeChat, this);
        RxUtils.bindEvents(mBtnConfirm, this);
        mIvAlipay.setTag(true);
        mIvWeChat.setTag(false);
        mDialog = new Dialog(this, R.style.dialog_loading);

        mDialog.setContentView(R.layout.progressbar_loading);
//        mDialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 初始化数据
     */
    private void initInfo() {
        stringExtra = getIntent().getStringExtra(Constants.ARGUMENT);
        mTvPrice.setText(stringExtra);
        receivePayResult();
    }


    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.ll_wallet_change_recharge_alipay:
                if (!(boolean) mIvAlipay.getTag()) {
                    mIvAlipay.setImageResource(R.mipmap.wallet_recharge_selected);
                    mIvWeChat.setImageResource(R.mipmap.wallet_recharge_unselected);
                    mIvAlipay.setTag(true);
                    mIvWeChat.setTag(false);
                }
                break;
            case R.id.ll_wallet_change_recharge_wechat:
                if (!(boolean) mIvWeChat.getTag()) {
                    mIvWeChat.setImageResource(R.mipmap.wallet_recharge_selected);
                    mIvAlipay.setImageResource(R.mipmap.wallet_recharge_unselected);
                    mIvWeChat.setTag(true);
                    mIvAlipay.setTag(false);
                }
                break;
            case R.id.btn_wallet_change_recharge_confirm:
                clickConfirm();
                break;
        }
    }

    private void clickConfirm() {
        mDialog.show();
        mPresenter.createOrder(((boolean) mIvAlipay.getTag() && !(boolean) mIvWeChat.getTag()) ? true : false, stringExtra);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(),msg,3);
    }

    @Override
    public void getAliPayResult() {

    }

    @Override
    public void getWeChatResult() {

    }

    @Override
    public void cancelLoading() {
        mDialog.dismiss();
    }

    @Override
    public void createOrderFail() {
        TipUtils.showToast(App.getInstance(), getString(R.string.wallet_change_recharge_order_fail));
    }

    private void receivePayResult() {
        Disposable disposable = RxBus.get().toFlowable(PayResultBean.class).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<PayResultBean>() {
            @Override
            public void accept(PayResultBean payResultBean) throws Exception {
                if (payResultBean != null) {
                    if (payResultBean.result == Constants.PAY_RESULT_SUCCESS) {
                        TipUtils.showToast(WalletChangeRechargeActivity.this, getString(R.string.wallet_change_recharge_success));
                        RxBus.get().post(new EventBean(Constants.EVENT_TYPE_REFRESH));
                        finish();
                    } else if (payResultBean.result == Constants.PAY_RESULT_FAIL) {
                        TipUtils.showToast(WalletChangeRechargeActivity.this, getString(R.string.wallet_change_recharge_fail));
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }
}
